﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using System.IO;
using System.Globalization;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Collections.Generic;


namespace JM.JCY.SharePointComponent.UserManagement.DeptList
{
    public partial class DeptListUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

         
            if (SubmitSortPkid != null && SubmitSortPkid.Count > 0)
            {
               SaveSubmitOrder();
                _dataTable = GetAllDepartment();
            }

          
            DataTable dt = mDataTable;
            RpList.DataSource = dt;
            RpList.DataBind();
        }
     

        public DataSetUserManagement.T_DepartmentDataTable GetAllDepartment()
        {
            DataSetUserManagement.T_DepartmentDataTable deptDT = new DataSetUserManagement.T_DepartmentDataTable();
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                deptDT = proxy.GetAllDepartment();
            }
            return deptDT;
        }

        public void UpdateDepartmentSortOrder(DataSetUserManagement.T_DepartmentDataTable dt)
        {
            
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                proxy.UpdateDepartmentSortOrder(dt);
            }
          
        }

        protected void SaveSubmitOrder()
        {
            if (SubmitSortPkid == null)
                return;
            if (SubmitSortPkid.Count <= 0)
                return;
            if (mDataTable == null)
                return;
            if (mDataTable.Rows.Count <= 0)
                return;


            mDataTable.PrimaryKey = new[] { mDataTable.Columns[0] }; //手动设置主键,与数据库中的主键设置与否无关
            int sortIndex = 1;
            foreach (int pkid in SubmitSortPkid)
            {
                DataRow dataRow = mDataTable.Rows.Find(pkid);
                if (dataRow != null)
                {
                    dataRow["SortOrder"] = sortIndex++;
                }
            }

            if (sortIndex > 1)
                UpdateDepartmentSortOrder(mDataTable);

            
        }

    
        protected ICollection<int> SubmitSortPkid
        {
            get
            {
                List<int> sortPkid = new List<int>();
                string s = Request["sortId"];
                if (!string.IsNullOrEmpty(s))
                {
                    string[] ss = s.Split(',', '|');
                    foreach (string s1 in ss)
                    {
                        int i = 0;
                        if (int.TryParse(s1, out i))
                        {
                            if (i > 0)
                                sortPkid.Add(i);
                        }
                    }
                }
                return sortPkid;
            }
        }

        private DataSetUserManagement.T_DepartmentDataTable _dataTable;
        protected DataSetUserManagement.T_DepartmentDataTable mDataTable
        {
            get
            {
                if (_dataTable == null)
                    _dataTable = GetAllDepartment();
                return _dataTable;
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("DeptMgr.aspx");
        }
       
    }
}
